<template>
  <div class="layout">
    <!-- 头部导航 -->
    <el-header class="header">
      <div class="header-content">
        <!-- Logo -->
        <div class="logo" @click="$router.push('/')">
          <div class="logo-icon">JD</div>
          <span class="logo-text">京东商城</span>
        </div>
        
        <!-- 搜索框 -->
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索商品..."
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button :icon="Search" @click="handleSearch">搜索</el-button>
            </template>
          </el-input>
        </div>
        
        <!-- 导航菜单 -->
        <el-menu
          :default-active="activeMenu"
          class="nav-menu"
          mode="horizontal"
          :ellipsis="false"
          @select="handleMenuSelect"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/products">商品分类</el-menu-item>
          <el-menu-item index="/cart">
            <el-badge :value="cartCount" :hidden="cartCount === 0">
              <el-icon><ShoppingCart /></el-icon>
              购物车
            </el-badge>
          </el-menu-item>
          <el-menu-item index="/orders" v-if="isLogin">我的订单</el-menu-item>
          <el-sub-menu index="user" v-if="isLogin">
            <template #title>
              <el-icon><User /></el-icon>
              {{ username }}
            </template>
            <el-menu-item index="/profile">个人中心</el-menu-item>
            <el-menu-item @click="handleLogout">退出登录</el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/login" v-if="!isLogin">登录</el-menu-item>
          <el-menu-item index="/register" v-if="!isLogin">注册</el-menu-item>
        </el-menu>
      </div>
    </el-header>
    
    <!-- 主体内容 -->
    <el-main class="main-content">
      <router-view />
    </el-main>
    
    <!-- 底部 -->
    <el-footer class="footer">
      <div class="footer-content">
        <div class="footer-section">
          <h4>关于我们</h4>
          <p>京东商城是专业的综合网上购物商城</p>
          <p>销售家电、数码通讯、电脑、家居百货等数万个品牌</p>
        </div>
        <div class="footer-section">
          <h4>客户服务</h4>
          <ul>
            <li>帮助中心</li>
            <li>售后服务</li>
            <li>在线客服</li>
            <li>联系我们</li>
          </ul>
        </div>
        <div class="footer-section">
          <h4>购物指南</h4>
          <ul>
            <li>购物流程</li>
            <li>支付方式</li>
            <li>配送方式</li>
            <li>退换货政策</li>
          </ul>
        </div>
        <div class="footer-section">
          <h4>联系方式</h4>
          <p>客服热线：400-888-8888</p>
          <p>工作时间：9:00-22:00</p>
          <p>邮箱：service@jindong.com</p>
        </div>
      </div>
      <div class="copyright">
        <p>© 2025 京东商城 版权所有 | Vue3 + Element Plus 项目实训</p>
      </div>
    </el-footer>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useCartStore } from '@/stores/cart'
import { Search, ShoppingCart, User } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const cartStore = useCartStore()

const searchKeyword = ref('')
const activeMenu = ref('/')

// 计算属性
const isLogin = computed(() => userStore.isLogin)
const username = computed(() => userStore.username)
const isAdmin = computed(() => userStore.isAdmin)
const cartCount = computed(() => cartStore.cartCount)

// 监听路由变化
watch(() => route.path, (newPath) => {
  activeMenu.value = newPath
}, { immediate: true })

// 如果已登录，获取购物车数量
if (isLogin.value) {
  cartStore.fetchCartList()
}

// 搜索商品
const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({
      path: '/products',
      query: { keyword: searchKeyword.value }
    })
  }
}

// 菜单选择
const handleMenuSelect = (index) => {
  if (index && index !== 'user') {
    router.push(index)
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    userStore.logout()
    cartStore.cartList = []
    cartStore.cartCount = 0
    router.push('/login')
  } catch (error) {
    // 取消操作
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: #e3393c;
  color: white;
  padding: 0;
  height: 80px !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  flex-shrink: 0;
}

.logo-icon {
  height: 40px;
  width: 40px;
  background: linear-gradient(135deg, #e23e3e 0%, #d41f1f 100%);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: white;
  box-shadow: 0 2px 8px rgba(226, 62, 62, 0.3);
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: white;
  white-space: nowrap;
}

.search-box {
  flex: 1;
  max-width: 500px;
}

.nav-menu {
  flex-shrink: 0;
  background: transparent;
  border: none;
}

.nav-menu .el-menu-item,
.nav-menu .el-sub-menu {
  color: white;
  border-bottom: none;
}

.nav-menu .el-menu-item:hover,
.nav-menu .el-menu-item.is-active {
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.main-content {
  flex: 1;
  padding: 20px;
  background: #f5f5f5;
  min-height: calc(100vh - 280px);
}

.footer {
  background: #333;
  color: #999;
  padding: 40px 20px 20px;
  height: auto !important;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 30px;
  margin-bottom: 30px;
}

.footer-section h4 {
  color: white;
  margin-bottom: 15px;
  font-size: 16px;
}

.footer-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.footer-section li {
  padding: 5px 0;
  cursor: pointer;
}

.footer-section li:hover {
  color: #e3393c;
}

.footer-section p {
  margin: 8px 0;
  line-height: 1.6;
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #444;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-wrap: wrap;
    height: auto;
    padding: 10px;
  }
  
  .logo-text {
    font-size: 18px;
  }
  
  .search-box {
    width: 100%;
    max-width: 100%;
    order: 3;
  }
  
  .nav-menu {
    font-size: 14px;
  }
  
  .footer-content {
    grid-template-columns: 1fr;
    gap: 20px;
  }
}
</style>

